<?php global $curUser; ?>
<script>
    function validate() {
        var reply_content = document.forms[1].reply_content;
        var submit = true;
        if (reply_content.value === "") {
            document.forms[1].reply_content.previousSibling.style.color = 'red';
            submit = false;
        } else {
            document.forms[1].reply_content.previousSibling.style.color = 'black';
        }
        if (submit) {
            document.forms[1].submit();
            reply_content.value = "";
        }
    }

    <?php if ($_SESSION['userid']) : ?>
    $(document).on('click', 'a#add_like', function(e) {
        var data = $(this).attr('data');
        var datas = data.split('_');
        if ($('div.group.fav').attr('class') !== "group fav marked") {
            var pl = new SOAPClientParameters();
            pl.add("statusid", datas[0]);
            pl.add("userid", datas[1]);
            pl.add("datecreated", datas[2]);
            SOAPClient.invoke("<?php echo $this->baseUrl; ?>m/", "addLike", pl, true, callback_addLike);
        } else {
            var pl = new SOAPClientParameters();
            pl.add("statusid", datas[0]);
            pl.add("userid", datas[1]);
            SOAPClient.invoke("<?php echo $this->baseUrl; ?>m/", "deleteLike", pl, true, callback_deleteLike);
        }
    });
    
    $(document).on('click', 'a.meta-act-link.meta-delete', function(e){
        var pl = new SOAPClientParameters();
        pl.add("statusid", "<?php echo $this->view->status->id; ?>");
        SOAPClient.invoke("<?php echo $this->baseUrl; ?>m/", "deleteStatus", pl, true, callback_deleteStatus);
    });
    
    function callback_deleteStatus(r){
        if(r === "true"){
            var pl = new SOAPClientParameters();
            pl.add("photoid", "<?php echo $this->view->model->id; ?>");
            SOAPClient.invoke("<?php echo $this->baseUrl; ?>m/", "deletePhotoData", pl, true, callback_deletePhotoData);
        }
    }
    
    function callback_deletePhotoData(r){
        if(r === "true"){
            window.location.href = "<?php echo $this->baseUrl.$curUser->username; ?>";
        }
    }
    <?php endif; ?>
    function callback_deleteLike(r) {
        if (r === "true") {
            $('a#add_like span').text("Like?");
            $('a#add_like').attr('class', 'meta-act-link meta-like fav-toggle');
            $('div.group.fav').attr('class', 'group fav');
            var datas = $('a#add_like').attr('data').split("_");
            var pl = new SOAPClientParameters();
            pl.add("statusid", datas[0]);
            SOAPClient.invoke("<?php echo $this->baseUrl; ?>m/", "getAmountLike", pl, true, callback_getAmountLike);
        }
    }

    function callback_addLike(r) {
        if (r !== 0) {
            var datas = $('a#add_like').attr('data').split("_");
            var pl = new SOAPClientParameters();
            pl.add("statusid", datas[0]);
            SOAPClient.invoke("<?php echo $this->baseUrl; ?>m/", "getAmountLike", pl, true, callback_getAmountLike);
            $('a#add_like span').text("Undo Like");
            $('a#add_like').attr('class', 'current-user-likes meta-act-link fav-toggle');
            $('div.group.fav').attr('class', 'group fav marked');
        }
    }

    function callback_getAmountLike(r) {
        if (r !== -1) {
            if (r === 1) {
                $('a#amount_like').text(r + " like");
            } else {
                $('a#amount_like').text(r + " likes");
            }
        }
    }
</script>
<?php if (!$this->view->model) { ?>
    <div class="null-message">
        <h2>The image you are looking for is not found.</h2>
        <p>Go back to <a href="<?php echo $this->baseUrl; ?>">Home</a>
        </p>
    </div>
<?php
} else {
    $this->view->status->increaseView();
    $photo = $this->view->model;
    if ($_POST['reply']) {
        $reply = Reply::withData($this->view->status->id, $curUser->id, $_POST['reply']['content'], date("Y-m-d"));
        $this->view->replies[] = Reply::addReply($reply);
    }
    $status = $this->view->status;
    $user = $status->user;
    $avatar = $status->avatar;
    $liked = Like::checkLike($status->id, $curUser->id);
    ?>
    <div id="main">
        <div id="screenshot-title-section" class="full title">
            <div class="single-title vcard group">
                <a href="<?php echo $this->baseUrl . $user->username; ?>" class="url" rel="contact" title="<?php echo $user->name; ?>"><div data-picture="" data-alt="<?php echo $user->name; ?>" data-class="photo">
                        <div data-src="<?php echo $this->baseUrl . $avatar->url; ?>"></div>
                        <div data-src="<?php echo $this->baseUrl . $avatar->url; ?>" data-media="(-webkit-min-device-pixel-ratio: 1.5),
                             (min--moz-device-pixel-ratio: 1.5),
                             (-o-min-device-pixel-ratio: 3/2),
                             (min-device-pixel-ratio: 1.5),
                             (min-resolution: 1.5dppx)"></div>
                        <img alt="<?php echo $user->name; ?>" class="photo" src="<?php echo $this->baseUrl . $avatar->url; ?>"></div>
                </a>
                <h1 id="screenshot-title"><?php echo $photo->title; ?></h1>
                <div class="shot-byline">
                    <div class="attribution ">
                        <span class="shot-byline-user">
                            by <a href="<?php echo $this->baseUrl . $user->username; ?>" class="url" rel="contact"><?php echo $user->name; ?></a>
                        </span>
                        <div class="follow-prompt">
                            <a href="<?php echo $this->baseUrl . $user->username; ?>/followers" class="follow">
                                <span>Follow</span>
                            </a><a href="<?php echo $this->baseUrl . $user->username; ?>/followers" class="unfollow">
                                <span>Following</span>
                            </a><a href="/blocks/<?php echo $user->username; ?>" class="unblock" data-hover="Unblock">Blocked</a>
                        </div>
                    </div>
                    <span class="screenshot-dash">
    <?php echo date("D M-d-Y", strtotime($status->datecreated)); ?>
                    </span>
                </div>
            </div>
        </div>

        <div class="the-shot" data-screenshot_id="1345706">
            <div class="single">
                <div class="single-grid"><div class="single-img">
                        <div data-picture="" data-alt="<?php echo $photo->title; ?>">
                            <div data-src="<?php echo $this->bseUrl . $photo->url; ?>"></div>
                            <div data-src="<?php echo $this->bseUrl . $photo->url; ?>" data-media="(-webkit-min-device-pixel-ratio: 1.5),
                                 (min--moz-device-pixel-ratio: 1.5),
                                 (-o-min-device-pixel-ratio: 3/2),
                                 (min-device-pixel-ratio: 1.5),
                                 (min-resolution: 1.5dppx)"></div>
                            <img alt="<?php echo $photo->title; ?>" src="<?php echo $this->baseUrl . $photo->url; ?>">
                        </div>
                    </div></div>
            </div>
        </div>

        <div class="screenshot-meta">
            <div id="like-section" class="meta-act logged-in">
                <div class="group fav <?php if ($liked) echo "marked"; ?>">
                    <strong class="fav-number">
                        <a id="amount_like"><?php
                        $amount = Like::getAmountLike($status->id);
                        if($amount != -1){
                            if($amount == 1 || $amount == 0) 
                                echo $amount." like";
                            else
                                echo $amount." likes";
                        }
                      ?></a>
                    </strong>
                    <a id="add_like" data="<?php echo $status->id . "_" . $curUser->id . "_" . date("Y-m-d"); ?>" class="<?php if ($liked) echo "current-user-likes meta-act-link fav-toggle";
                            else echo "meta-act-link meta-like fav-toggle"; ?>" title="<?php if ($liked) echo "You like this screenshot. Click to undo.";
                            else echo "Like this screenshot?"; ?>">
                        <span><?php if ($liked) echo "Undo Like";
                            else echo "Like?"; ?></span>
                    </a></div>
            </div>
            <div class="meta-act meta-act-views">
                <span class="meta-act-link meta-views"><?php echo $status->view; ?> <em>views</em></span>
            </div>
            <?php if ($status->user->id == $curUser->id) : ?>
            <div class="social-media meta-act meta-act-full">
                <a href="#" class="meta-act-link meta-delete">Delete</a>
            </div>
            <?php endif; ?>
            <div class="meta-act meta-act-full">
                <div class="meta-act-link meta-colors">
                    <ul class="color-chips group">
                        <?php
                        echo Color::getColorBarNotDefault($photo->url);
                        ?>
                    </ul>
                </div>
            </div>
        </div>
        <?php if ($_SESSION['userid']) : ?>
        <div id="comments-section">
            <h2 class="count section ">
                <?php $replies = Reply::getRepliesCount($status->id);
                    if($replies != -1) echo $replies; ?> Responses
            </h2>

            <ol id="comments" class="comments">
                <?php
                if (is_array($this->view->replies))
                    foreach ($this->view->replies as $reply) {
                        $user_reply = $reply->user;
                        $avatar_reply = $reply->user->avatar;
                        ?>
                        <li id="comment-3189966" class="response comment group " data-user-id="23085">
                            <h2>
                                <a href="<?php echo $this->baseUrl.$user_reply->username ?>" class="url" rel="contact" title="<?php echo $user_reply->name ?>"><img alt="<?php echo $user_reply->name ?>" class="photo" src="<?php echo $this->baseUrl.$avatar_reply->url ?>"><?php echo $user_reply->name ?></a>
                            </h2>
                            <div class="comment-body">
                                <p><?php echo $reply->content; ?></p>
                            </div>
                            <p class="comment-meta">
                                <a href="#comment-3189966" class="posted"><?php echo date("D M-d-Y", strtotime($reply->datecreated)) ?></a>
                            </p>
                        </li>
            <?php
                }
            ?>
            </ol>
            <form onsubmit="return false;" accept-charset="UTF-8" action="" class="account gen-form with-messages" id="upload_photo" method="post" style="float: left; width: 450px;"> 
                <div class="form-field form-field-bio">
                    <fieldset class="reply_content"><label for="reply_content" style="width: 70px;">Reply</label><textarea class="bio" cols="40" id="reply_content" name="reply[content]" rows="3" style="resize: none; width: 320px;" maxlength="160"></textarea></fieldset>
                </div>
                <div class="form-btns">
                    <input style="margin-left: 50px;" class="form-sub" name="reply" type="submit" value="Reply" onclick="validate();">
                </div>
            </form>
        </div>
        <?php endif; ?>
    </div>
<?php } ?>
